package g11.testdao;

import g11.dao.AppointmentDAO;
import g11.dao.AppointmentDAOImpl;
import g11.dao.DAOException;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.dao.PatientDAO;
import g11.dao.PatientDAOImpl;
import g11.dao.VisitationContentDAO;
import g11.dao.VisitationContentDAOImpl;
import g11.dao.VisitationDAO;
import g11.dao.VisitationDAOImpl;
import g11.model.Appointment;
import g11.model.Employee;
import g11.model.Patient;
import g11.model.Visitation;
import g11.model.VisitationContent;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.Test;

public class testVisitationContentDao extends TestCase {

	@Test
	public void testVisitationContentCRUD() throws DAOException {
		// Create and persistent Employee
		Employee employee = new Employee();
		employee.setUsername("penny");
		employee.setPassword("1234");
		Byte type = 1;
		employee.setType(type);
		employee.setName("Penny Money");
		employee.setDepartment("Surgery");
		employee.setTitle("Surgery Doctor");
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		employeeDao.createEmployee(employee);
		Employee retrievedEmployee = new Employee();
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");

		// Create and persistent Patient
		Patient patient = new Patient();
		Patient retrievedPatient = new Patient();
		patient.setUsername("jhope");
		patient.setName("Julia Hope");
		patient.setPassword("1234");
		type = 0;
		patient.setType(type);
		patient.setPhone("519-888-8888");
		patient.setAddress("77 Weber Street");
		patient.setOhip("2222222222ka");
		patient.setSin("1111111111");
		patient.setCurrentHealth("Diabetes.");
		PatientDAO patientDao = new PatientDAOImpl();
		Integer patientId = patientDao.createPatient(patient);
		retrievedPatient = patientDao.getPatientById(patientId);

		// create and persistent Appointment
		Appointment appointment = new Appointment();
		appointment.setDoctor(retrievedEmployee);
		appointment.setPatient(retrievedPatient);
		AppointmentDAO appointmentDao = new AppointmentDAOImpl();
		Integer appointmentId;
		appointmentId = appointmentDao.createAppointment(appointment);
		Appointment retrievedAppointment = new Appointment();
		retrievedAppointment = appointmentDao.getAppointmentById(appointmentId);

		// create and persistent Visitation
		Visitation visitation = new Visitation();
		VisitationDAO visitationDao = new VisitationDAOImpl();
		visitationDao.createVisitationFromAppointment(retrievedAppointment);
		Visitation retrievedVisitation = new Visitation();
		retrievedVisitation = visitationDao
				.getCurrentVersionOfVisitation(appointmentId);

		// create and persistent VisitationContent;
		VisitationContent visitationContent = new VisitationContent();
		visitationContent.setVisitation(retrievedVisitation);
		visitationContent.setComment("nothing c");
		visitationContent.setDiagnosis("nothing a");
		visitationContent.setTreatment("nothing b");
		java.util.Date date = new java.util.Date();
		visitationContent.setLastModified(new Timestamp(date.getTime()));
		visitationContent.setVersion(retrievedVisitation.getCurrentVersion());
		VisitationContentDAO visitationContentDao = new VisitationContentDAOImpl();
		Integer visitationContentId = visitationContentDao
				.createVisitationContent(visitationContent);

		// // verification if persistence success;
		// assertEquals(retrievedVisitation.getVisitationId(),
		// visitation.getVisitationId());
		// assertEquals(retrievedVisitation.getCurrentVersion(),
		// visitation.getCurrentVersion());

		// // create and persistent another VisitationContent;
		// VisitationContent anotherVisitationContent = new VisitationContent();
		// anotherVisitationContent = visitationContent;
		// anotherVisitationContent.setComment("something c");
		// anotherVisitationContent.setDiagnosis("something a");
		// anotherVisitationContent.setTreatment("something b");
		// anotherVisitationContent.setLastModified(new
		// Timestamp(date.getTime()));
		// visitation = visitationContentDao
		// .createVisitationContentWithVersion(anotherVisitationContent);
		// visitationDao.updateCurrentVersion(visitation);
		//
		// // verify if persistence success;
		// assertEquals(visitation.getVisitationId(),
		// retrievedVisitation.getVisitationId());
		// assertFalse(visitation.getCurrentVersion().equals(
		// retrievedVisitation.getCurrentVersion()));
		// assertEquals(visitation.getCurrentVersion(), visitationDao
		// .getCurrentVersionOfVisitation(visitation.getVisitationId())
		// .getCurrentVersion());

		// verify retrival of entries
		// List<VisitationContent> retrievedVisitationContentList = new
		// ArrayList<VisitationContent>();
		VisitationContent retrievedVisitationContent = new VisitationContent();
		retrievedVisitationContent = visitationContentDao
				.getVisitationContentById(visitationContentId);

		assertEquals(retrievedVisitationContent.getComment(), "nothing c");
		assertEquals(retrievedVisitationContent.getDiagnosis(), "nothing a");
		assertEquals(retrievedVisitationContent.getTreatment(), "nothing b");

		// assertEquals(retrievedVisitationContentList.size(), 2);
		// assertEquals(retrievedVisitationContentList.get(1).getComment(),
		// "something c");
		// assertEquals(retrievedVisitationContentList.get(1).getDiagnosis(),
		// "something a");
		// assertEquals(retrievedVisitationContentList.get(1).getTreatment(),
		// "something b");
		// assertEquals((int)
		// retrievedVisitationContentList.get(1).getVersion(),
		// 2);
		// assertEquals(
		// visitationContentDao.getCurrentVisitationContent(visitation)
		// .getComment(), "something c");
		// assertEquals(
		// visitationContentDao.getCurrentVisitationContent(visitation)
		// .getDiagnosis(), "something a");
		// assertEquals(
		// visitationContentDao.getCurrentVisitationContent(visitation)
		// .getTreatment(), "something b");

		// // verify the contained set of visitation
		// assertEquals(
		// visitationContentDao.getCurrentVisitationContent(visitation)
		// .getVisitation().getAppointment().getDoctor().getName(),
		// "Penny Money");

		// test query by keyword
		List<VisitationContent> keywordVisitationContentList = new ArrayList<VisitationContent>();
		keywordVisitationContentList = visitationContentDao
				.getVisitationContentByKeyword("a", 0);
		assertEquals(keywordVisitationContentList.get(0).getDiagnosis(),
				"nothing a");
		keywordVisitationContentList = visitationContentDao
				.getVisitationContentByKeyword("b", 1);
		assertEquals(keywordVisitationContentList.get(0).getTreatment(),
				"nothing b");
		keywordVisitationContentList = visitationContentDao
				.getVisitationContentByKeyword("c", 2);
		assertEquals(keywordVisitationContentList.get(0).getComment(),
				"nothing c");
		keywordVisitationContentList = visitationContentDao
				.getVisitationContentByKeyword("Julia", 3);
		assertEquals(keywordVisitationContentList.get(0).getVisitation()
				.getAppointment().getPatient().getUsername(), "jhope");

		// delete entries
		visitationContentDao
				.deleteVisitationContent(retrievedVisitationContent);
		visitationDao.deleteVisitation(retrievedVisitation);
		appointmentDao.deleteAppointment(retrievedAppointment);
		employeeDao.deleteEmployee(retrievedEmployee);
		patientDao.deletePatient(retrievedPatient);

	}

}
